import dTalkApi from "../core/dTalkApi";
import * as moment from 'moment';
import temporaryDataModel from "../model/temporaryDataModel";
import dTalkApiUtil from 'dingtalk-app/dtalk/dTalkApiUtil';
//暂存拜访记录数据
class DataService{
    constructor(){}
    //保存暂存数据
    async saveTemData(res:any){
        let json:any=[];
        let access_token=await dTalkApiUtil.getAccessToken();
        let employeeInfo=await dTalkApi.getUserDetail(res.userId,access_token)
        let newProductList:any[]=[];
        let sampleList:any[]=[];
        let mouseList:any[]=[];
        if(res.plantype=='门店'){
            //鼠标
            if(res.mouseList.length==0){
                mouseList=[{
                    mousetype:'',
                    mouseCount:'',
                    mousePicture:''
                }]
            }else{
                res.mouseList.map(it=>{
                    mouseList.push({
                        mousetype:it.mousetype,
                        mouseCount:it.mouseCount,
                        mousePicture:it.mousePicture
                    })
                })
            }
            //样机
            if(res.sampleList.length==0){
                sampleList.push({
                    sampleType:'',//样机型号
                    Picturesample:''  //样机图片
                })
            }else{
                res.sampleList.map(it=>{
                    sampleList.push({
                        sampleType:it.sampleType,
                        Picturesample:it.Picturesample
                        
                    })
                })
            }
            //新品上架
            if(res.newProductList.length==0){
                newProductList.push({
                    newProductType:'',//样机型号
                    pictureNewProduct:''  //样机图片
                })
            }else{
                res.newProductList.map(it=>{
                    newProductList.push({
                        newProductType:it.newProductType,
                        pictureNewProduct:it.pictureNewProduct
                        
                    })
                })
            }
        }
        let data=await temporaryDataModel.find({userId:res.userId});
       
        if(data.length !=0){
            let re=await temporaryDataModel.remove({userId:res.userId});
        }
        console.log(res.userId+'无暂存数据!添加开始!!!!!!!!!')
        let ad=await temporaryDataModel.update({userId:''},{
            month:moment(res.planDate).format('MM'),
            year:moment(res.planDate).format('YYYY'),
            number:res.userId+'%%'+res.planDate,
            userId:res.userId,//员工id
            jobnumber:employeeInfo.jobnumber,//员工工号
            plantype:res.plantype,//拜访类型
            planPurpose:res.planPurpose,//计划的拜访目的
            name:res.consumerName,//名称
            code:res.consumerCode,//编码
            date:res.planDate,//拜访日期
            location:res.location,//签到地址   
            pic:res.picture,//图片
            //客户
            way:res.visitWay,//拜访方式
            recoderPurpose:res.recoderPurpose,
            talkPersonName:res.talkPersonName,
            talkPersonJob:res.talkPersonJob,
            connection:res.connection,
            visitResult:res.visitResult,
            followMatter:res.followMatter,
            cooperation:res.cooperation,
            //门店
            sampleList:sampleList,//样机列表[]
            storeType:res.storeType,
            isListSample:res.isListSample,
            isListStandard:res.isListStandard,
            competitor:res.competitor,
            storeActivity:res.storeActivity,
            pictureStoreActivity:res.pictureStoreActivity,
            listLetter:res.listLetter,
            information:[{
                mouseList:mouseList,//鼠标
                newProductList:newProductList,//新品上架
                isListNewProduct:res.isListNewProduct,
                keyBoardType:res.keyBoardType,
                pictureBrand:res.pictureBrand,//品牌专区照片
                brand:res.brand,//品牌专区
                isListPart:res.isListPart,
                pictureFullContainer:res.pictureFullContainer,
                // storeFigure:res.storeFigure,//店内形象
                pictureStoreFigure:res.pictureStoreFigure,//形象照片
                brandPicture:res.brandPicture,//品牌专区形象照片2
                picturePosm:res.picturePosm,//POSM形象照片
                pictureFrame:res.pictureFrame,//画面形象照片
                storeTypes:res.storeTypes,
                quarterNecessarySUK:res.quarterNecessarySUK,
                pictureSUK:res.pictureSUK,
                pictureListFocus:res.pictureListFocus,//集中陈列图片
                isListFocus:res.isListFocus
            }]

        },{upsert:true})
        console.log(ad,'add_add_add_add_add_add_add_add_');
        return ad;
    }

    //提取暂存数据
    async getTemporaryData(employeeId:string,date:string){
        let data=await temporaryDataModel.findOne({userId:employeeId,date:date});
        return data;
    }
    
    //一个月清除一次暂存的拜访记录数据
    async deleteTemporaryData(monthList:any[]){
        console.log('清除多个月的暂存数据开始!!!!!!!!!!!!!!')
        console.log('月份列表:'+monthList)
        let r=await temporaryDataModel.remove({month:{$in:monthList}});
        console.log('清除多个月的暂存数据结束!!!!!!!!!!!!!!',r)
      
    }



}
let dataService=new DataService();
export default dataService;